﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using TrackerRealm.Jazz.Common;

namespace TrackerRealm.Jazz.Client
{
    /// <summary>
    /// An immutable collection of '<see cref="jObject"/>'s.
    /// </summary>
    public class jSet<T>: IEnumerable<T>, ICollection<T> where T: jObject
    {
        private T[] jazzObjArray;
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="jazzObjs"></param>
        public jSet(IEnumerable<T> jazzObjs)
        {
            if (jazzObjs.Distinct().Count() != jazzObjs.Count())
                throw new ArgumentException("Elements are not unique.", "jazzObjs");
            this.jazzObjArray = jazzObjs.ToArray();
        }
        
        #region IEnumerable<T> Members
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<T> GetEnumerator()
        {
            return ((IEnumerable<T>)this.jazzObjArray).GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion

        #region ICollection<T> Members
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public void Add(T item)
        {
            throw new System.NotSupportedException();
        }
        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            throw new NotSupportedException();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(T item)
        {
            return this.jazzObjArray.Contains(item);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public bool Contains(string roleName)
        {
            return this.jazzObjArray.First(r => r.Name == roleName) != null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(T[] array, int arrayIndex)
        {
            if (object.ReferenceEquals(array, null))
            {
                throw new ArgumentNullException(
                    "Null array reference",
                    "array"
                    );
            }

            if (arrayIndex < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "Index is out of range",
                    "arrayIndex"
                    );
            }

            if (array.Rank > 1)
            {
                throw new ArgumentException(
                    "Array is multi-dimensional",
                    "array"
                    );
            }

            foreach (object o in this)
            {
                array.SetValue(o, arrayIndex);
                arrayIndex++;
            }
        }
        /// <summary>
        /// Returns the number of members in this collection.
        /// </summary>
        public int Count
        {
            get { return jazzObjArray.Length; }
        }
        /// <summary>
        /// 
        /// </summary>
        public bool IsReadOnly
        {
            get { return true; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(T item)
        {
            throw new NotSupportedException();
        }

        #endregion

       
    }
}
